﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace NTTDATA.BEENChina.ResultCertification.WcfServiceLibrary
{
	[ServiceBehavior(  InstanceContextMode = InstanceContextMode.PerSession	)]   
	public class LogService:ILogService  
	{

        public static Dictionary<string, IWriteLogCallback> ClientCallbacks = new Dictionary<string, IWriteLogCallback>();  
 

        public void SendMessage(  string msg, DateTime sendTime)
        {
            foreach (IWriteLogCallback c in LogService.ClientCallbacks.Values.ToArray())
            {
                if (c != null)
                {
                    c.SendToClients(  msg, sendTime);
                }
            }
        }  

		public void Subscribe()
		{
            string sessionID = OperationContext.Current.SessionId;
            IWriteLogCallback cb = OperationContext.Current.GetCallbackChannel<IWriteLogCallback>();
            LogService.ClientCallbacks[sessionID] = cb; 
 
		}

		public void Unsubscribe()
		{
            string sessionID = OperationContext.Current.SessionId;
            if (LogService.ClientCallbacks.ContainsKey(sessionID))
            {
                LogService.ClientCallbacks.Remove(sessionID);
            } 
		}

 
	} 
   
 
}
